package com.itljm.cxrMarket.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itljm.cxrMarket.common.PageResult;
import com.itljm.cxrMarket.common.Result;
import com.itljm.cxrMarket.entity.Merchants;
import com.itljm.cxrMarket.entity.Products;
import com.itljm.cxrMarket.entity.dto.ReasonRequestBody;
import com.itljm.cxrMarket.service.MerchantsService;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ljm
 * @date 2024/12/16 17:40
 */
@RestController
@RequestMapping("/merchants")
@CrossOrigin
public class MerchantsController {
    @Autowired
    private MerchantsService merchantsService;

    // 分页查询商户信息
    @RequestMapping("/info")
    public PageResult<Merchants> selectMerchantByPage(Integer page, Integer limit,
                                                      String merchantName,
                                                      String categoryId,
                                                      String merchantStatus) {
        if (page == null) {
            page = 0;
        }
        if (limit == null) {
            limit = 15;
        }
        Page<Products> productsPage = new Page<>(page, limit);
        QueryWrapper<Products> wrapper = new QueryWrapper<>();
        wrapper.eq("1",1);
        if (StringUtils.isNotBlank(merchantName)) {
            wrapper.like("merchant_name", merchantName);
        }
        if(StringUtils.isNotBlank(categoryId)) {
            wrapper.eq("m.category_id", categoryId);
        }
        if(StringUtils.isNotBlank(merchantStatus)) {
            wrapper.eq("merchant_status", merchantStatus);
        }

        List<Merchants> merchants = merchantsService.selectMerchantByPage(productsPage, wrapper);
        return new PageResult<>(merchants, productsPage.getTotal());
    }

    // 根据商户ID查询商户信息
    @RequestMapping("/info/{id}")
    public Result selectMerchantById(@PathVariable("id") Integer merchantId) {
        Merchants merchants = merchantsService.selectMerchantById(merchantId);
        if (merchants != null) {
            return Result.ok(merchants);
        }
        return Result.fail("查询失败");
    }

    // 根据用户ID查询商户信息
    @RequestMapping("/my/{userId}")
    public Result selectMerchantByUserId(@PathVariable Long userId) {
        Merchants merchants = merchantsService.selectMerchantByUserId(userId);
        if (merchants != null) {
            return Result.ok(merchants);
        }
        return Result.fail("查询失败");
    }

    // 新增或修改商户信息
    @RequestMapping("/save")
    public Result saveOrUpdateMerchant(@RequestBody Merchants merchant) {
        boolean b = merchantsService.saveOrUpdateMerchant(merchant);
        if (b) {
            return Result.ok("操作成功");
        }
        return Result.fail("操作失败");
    }

    // 删除商户信息
    @RequestMapping("/delete/{id}")
    public Result deleteMerchant(@PathVariable("id") Integer merchantId) {
        boolean b = merchantsService.removeById(merchantId);
        if (b) {
            return Result.ok("删除成功");
        }
        return Result.fail("删除失败");
    }

    // 审核商户
    @RequestMapping("/check/{id}/{status}")
    public Result auditMerchant(@PathVariable("id") Integer merchantId, @PathVariable("status") Integer status,
                                @RequestBody ReasonRequestBody requestBody) {
        String reason = requestBody != null ? requestBody.getReason() : null;
        boolean b = merchantsService.checkMerchantStatusById(merchantId, status, reason);
        if (b) {
            return Result.ok("操作成功");
        }
        return Result.fail("操作失败");
    }

    // 根据商铺名称查询商铺
    @RequestMapping("/search")
    public Result searchMerchantByName(@RequestParam("merchantName") String merchantName) {
        List<Merchants> list = merchantsService.selectMerchantByName(merchantName);
        if (list.size() > 0) {
            return Result.ok(list);
        }
        return Result.fail("查询失败");
    }
}
